﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Diagnostics;

using SharpEnviro.Interop;

namespace SharpEnviro.Desktop
{
	/// <summary>
	/// Interaction logic for DesktopObject.xaml
	/// </summary>
	public partial class DesktopObject : UserControl
	{
		/// <summary>
		/// Default constructor.
		/// </summary>
		public DesktopObject()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Create a new desktop file object for the specified file path.
		/// </summary>
		/// <param name="path">The path to the file use to create the desktop file object.</param>
		/// <param name="type">The type of desktop object being created.</param>
		public DesktopObject(string path, DesktopObjectType type)
			: this()
		{
			_settings = new DesktopObjectSettings(path, type);
		}

		/// <summary>
		/// The desktop object settings associated with this control.
		/// </summary>
		public DesktopObjectSettings Settings
		{
			get
			{
				return _settings;
			}
		}

		private void UserControl_Loaded(object sender, RoutedEventArgs e)
		{
			if (_settings.Path == null)
				return;

			LoadImage();
			SetToolTip();
			textBlock1.Text = System.IO.Path.GetFileName(_settings.Path);
		}

		private void LoadImage()
		{
			image1.Source = PInvoke.GetIcon(_settings.Path);
			return;

			using (System.Drawing.Icon ico = PInvoke.GetLargeIcon(_settings.Path))
			{
				image1.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
					ico.Handle,
					Int32Rect.Empty,
					BitmapSizeOptions.FromEmptyOptions());
			}

			//using (System.Drawing.Icon ico = System.Drawing.Icon.ExtractAssociatedIcon(_settings.Path))
			//{
			//    image1.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
			//        ico.Handle,
			//        Int32Rect.Empty,
			//        BitmapSizeOptions.FromEmptyOptions());
			//}
		}

		private void SetToolTip()
		{
			if (_settings.IsFile)
			{
				FileInfo fi = new FileInfo(_settings.Path);

				ToolTip = String.Format(
					"{0}\r\nDate Created: {1}\r\nDate Modified: {2}\r\nSize: {3}",
					fi.Name,
					fi.CreationTime,
					fi.LastWriteTime,
					fi.Length);
			}

			if (_settings.IsDirectory)
			{
				DirectoryInfo di = new DirectoryInfo(_settings.Path);

				ToolTip = String.Format(
					"{0}\r\nDate Created: {1}\r\nDate Modified: {2}",
					di.Name,
					di.CreationTime,
					di.LastWriteTime);
			}
		}

		private void UserControl_MouseDown(object sender, MouseButtonEventArgs e)
		{
			border1.Focus();

			if (e.ClickCount == 2 && e.LeftButton == MouseButtonState.Pressed)
				Process.Start(_settings.Path);

			e.Handled = true;
		}

		private void UserControl_PreviewMouseDown(object sender, MouseButtonEventArgs e)
		{
			//_mouseDragStart = e.GetPosition(this);
			//return;

			_mouseDragStart = e.GetPosition(null);
			_canvasLeft = Canvas.GetLeft((DesktopObject)sender);
			_canvasTop = Canvas.GetTop((DesktopObject)sender);
			((DesktopObject)sender).CaptureMouse();
		}

		//private bool _dragging = false;

		private void UserControl_PreviewMouseMove(object sender, MouseEventArgs e)
		{
			//if (e.LeftButton == MouseButtonState.Pressed && !_dragging)
			//{
			//    Point mousePos = e.GetPosition(this);

			//    if (Math.Abs(mousePos.X - _mouseDragStart.X) > SystemParameters.MinimumHorizontalDragDistance ||
			//        Math.Abs(mousePos.Y - _mouseDragStart.Y) > SystemParameters.MinimumVerticalDragDistance)
			//    {
			//        //StartDrag(e);
			//        CaptureMouse();
			//        _dragging = true;
			//        //DataObject data = new DataObject(System.Windows.DataFormats.Text.ToString(), "abcd");
			//        DragDropEffects de = DragDrop.DoDragDrop(this, this, DragDropEffects.Move);
			//        _dragging = false;
			//        ReleaseMouseCapture();
			//    }
			//}
			//return;
			if (((DesktopObject)sender).IsMouseCaptured)
			{
				Point mousePos = e.GetPosition(null);
				double left = mousePos.X - _mouseDragStart.X;
				double top = mousePos.Y - _mouseDragStart.Y;
				_mouseDragStart = e.GetPosition(null);
				((DesktopObject)sender).SetValue(Canvas.LeftProperty, _canvasLeft + left);
				((DesktopObject)sender).SetValue(Canvas.TopProperty, _canvasTop + top);
				_canvasLeft = Canvas.GetLeft((DesktopObject)sender);
				_canvasTop = Canvas.GetTop((DesktopObject)sender);
			}
		}

		private void UserControl_PreviewMouseUp(object sender, MouseButtonEventArgs e)
		{
			//return;
			((DesktopObject)sender).ReleaseMouseCapture();
		}

		private void UserControl_LostMouseCapture(object sender, MouseEventArgs e)
		{
			//return;
			((DesktopObject)sender).ReleaseMouseCapture();
		}

		private DesktopObjectSettings _settings;
		private Point _mouseDragStart;
		private double _canvasLeft;
		private double _canvasTop;

		private void UserControl_GotFocus(object sender, RoutedEventArgs e)
		{
			var control = sender as UIElement;
			if (control != null)
				Canvas.SetZIndex(control, 1);
		}

		private void UserControl_LostFocus(object sender, RoutedEventArgs e)
		{
			var control = sender as UIElement;
			if (control != null)
				Canvas.SetZIndex(control, 0);
		}
	}
}
